package com.webledger.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.naming.ldap.Rdn;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import com.webledger.db.DBClass;

public class LoginAction extends ActionSupport implements SessionAware {

	String username, password, Query;
	int userID = 0;
	Map m;
	Connection con = DBClass.getDB().getConnection();
	ResultSet rsVoucher;
	private String ledgervoucherType;
	private ArrayList<VoucherType> vouchertype;

	public String defalutVoucherType() {
		return "Debit";
	}

	public String getLedgervoucherType() {
		return ledgervoucherType;
	}

	public void setLedgervoucherType(String ledgervoucherType) {
		this.ledgervoucherType = ledgervoucherType;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() {

		Query = "select usercode,username,password from user where username='"
				+ username + "' and password='" + password + "'";
		String str = "";
		PreparedStatement ps;
		try {
			ps = con.prepareStatement(Query);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				userID= rs.getInt(1);
				GetVoucherType();

				str = "success";
			} else {
				addActionError("Invalid Username/Password");
				str = "error";

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addActionError("Exception Occured");
			return "error";
		}
		return str;

	}

	public void GetVoucherType() {
		String VoucherQuery = "select VoucherTypeNo,VoucherTypeName from VoucherType order by VoucherTypeName";
		try {
			PreparedStatement psVoucher = con.prepareStatement(VoucherQuery);
			rsVoucher = psVoucher.executeQuery();
			vouchertype = new ArrayList<VoucherType>();
			while(rsVoucher.next()){
				vouchertype.add(new VoucherType(rsVoucher.getString(2), rsVoucher.getInt(1)));
			}
			rsVoucher.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch bloc
			e.printStackTrace();
		
			
			
		}

	}

	public ArrayList<VoucherType> getVouchertype() {
		return vouchertype;
	}

	public void setVouchertype(ArrayList<VoucherType> vouchertype) {
		this.vouchertype = vouchertype;
	}

	public void setSession(Map m) {
		this.m=m;
		
	}
}
